package net.kem.android.positioner.custom;

import net.kem.android.positioner.MessageObject;
import net.kem.android.positioner.PositionerService;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

public class ServerMessageSwitchSlider extends ServerMessageSwitch implements SeekBar.OnSeekBarChangeListener {
	private TextView mProgressText;
	private SeekBar seekBar;

	public ServerMessageSwitchSlider(PositionerService.LocalBinder binder, Context context, View parent, int imageOffResouceId, int imageOnResouceId, MessageObject messageObject) {
		this(binder, context, (AttributeSet) null, parent, imageOffResouceId, imageOnResouceId, messageObject);
	}

	public ServerMessageSwitchSlider(PositionerService.LocalBinder binder, Context context, AttributeSet attrs, View parent, int imageOffResouceId, int imageOnResouceId, MessageObject messageObject) {
		super(binder, context, attrs, imageOffResouceId, imageOnResouceId, messageObject);

		mProgressText = new TextView(context, attrs);
		seekBar = new SeekBar(context, attrs);
		seekBar.setMax(10);
		int progress = Integer.parseInt(messageObject.getState());
		setState(progress, false);
		seekBar.setProgress(progress);
		seekBar.setLayoutParams(parent.getLayoutParams());
		seekBar.setOnSeekBarChangeListener(this);

		addView(mProgressText);
		addView(seekBar);
	}

	public void setState(int progress, boolean doSend) {
		messageObject.setState(String.valueOf(progress));
		boolean checked = progress > 0;
		super.setChecked(checked, doSend);
	}

	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
		mProgressText.setText(String.valueOf(progress));
	}

	public void onStartTrackingTouch(SeekBar seekBar) {
		//mTrackingText.setText(getString(R.string.seekbar_tracking_on));
	}

	public void onStopTrackingTouch(SeekBar seekBar) {
		//mTrackingText.setText(getString(R.string.seekbar_tracking_off));
		int progress = seekBar.getProgress();
		seekBar.setSecondaryProgress(progress); // set the shade of the previous value.
		setState(progress, true);
	}

	public int getProgress() {
		return seekBar.getProgress();
	}

	public void setProgress(int value) {
		seekBar.setProgress(value);
	}
}